package com.qf.controller;

import com.github.pagehelper.PageInfo;
import com.qf.entity.*;
import com.qf.enumutil.SizeEnum;
import com.qf.service.PositionService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 2019/11/22
 * 郭程鹏
 * gcp1909
 * 面向代码面向君,不负时光不负卿
 */
@Controller
public class PositionController {

    @Autowired
    private PositionService positionService;

    //得到全部职位信息
    @RequestMapping("/resource/demo3/list")
    @RequiresPermissions("pre:demo3list")
    public String allPosition(Model model, @RequestParam(defaultValue = "1") Integer page, String roleName, String deptName, String status){

        Map<String, String > map = new HashMap<>();
        map.put("roleName",roleName);
        map.put("deptName",deptName);
        map.put("status",status);

        PageInfo<UserRole> pageInfo = positionService.allPosition(page, SizeEnum.pageSize.getSize(), map);

        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("roleName",roleName);
        model.addAttribute("deptName",deptName);
        model.addAttribute("status",status);

        return "resource/demo3/list";
    }

    //根据角色编号查询角色信息
    @RequestMapping("/resource/demo3/view")
    @RequiresPermissions("pre:demo3view")
    public String posiDesc(@RequestParam("roleId") Integer roleId,Model model){

        UserRole userRole = positionService.roleDesc(roleId);

        model.addAttribute("role",userRole);

        return "resource/demo3/view";
    }

    //添加角色时回显所有菜单 职位分类
    @RequestMapping("/resource/demo3/preadd")
    @RequiresPermissions("pre:demo3preadd")
    public String preAdd(Model model){
        List<SystemMnue> mnues = positionService.getMnues();
        model.addAttribute("munes",mnues);
        List<PositionClass> position = positionService.getPosition();
        model.addAttribute("position",position);
        List<Department> allDept = positionService.getAllDept();
        model.addAttribute("dept",allDept);
        String roleId = positionService.creatUserId();
        model.addAttribute("roleId",roleId);
        return "resource/demo3/add";
    }

    //添加角色信息、增加角色描述、添加角色与菜单之间的关系
    @RequestMapping("/resource/demo3/add")
    @RequiresPermissions("pre:demo3add")
    public String roleAdd(UserRole role, UserPosition userPosition,@RequestParam("mnueId") List<Integer> mnueId){

        System.out.println(role);
        System.out.println(userPosition);
        System.out.println(mnueId);
        positionService.addToRole(role,mnueId,userPosition);

        //重定向到一个请求
        return "redirect:/resource/demo3/list";

    }

    //根据角色id删除角色,以及角色对应的菜单和功能
    @RequestMapping("/resource/demo3/deleRole")
    @ResponseBody
    @RequiresPermissions("pre:demo3deleRole")
    public MyJson deleteRole(@RequestParam("roleId") String roleId){
        int i = positionService.deleteRole(roleId);
        if (i>0) {
            MyJson myJson = new MyJson();
            myJson.setMessage("修改成功");
            return myJson;
        }else {
            MyJson myJson = new MyJson();
            myJson.setMessage("修改失败");
            return myJson;
        }
    }

    //根据角色批量删除数据
    @Transactional
    @RequestMapping("/resource/demo3/deleteAllRole")
    @RequiresPermissions("pre:demo3deleteAllRole")
    public void deleteAllRole(@RequestParam("roleId") List<Integer> roleId, HttpServletResponse response){
        response.setContentType("text/html;charset=utf-8");
        int i = positionService.deleteAllRole(roleId);
        if (i>0){
            try {
                response.getWriter().print("<script>alert('删除成功');location.href='/resource/demo3/list';</script>");
            } catch (IOException e) {
                e.printStackTrace();
                try {
                    response.getWriter().print("<script>alert('系统繁忙');location.href='/resource/demo3/list'</script>");
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }else {
            try {
                response.getWriter().print("<script>alert('删除失败');location.href='/resource/demo2/list'</script>");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
